﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Sustainalytics.ScreeningTool.Synchronization
{
    public static class Log
    {
        private static Action<string> _log = Console.WriteLine;

        public static void Initialize(Action<string> log) => _log = log;

        public static void Info(string message) => _log(message);

        public static void Warning(string message) => _log("Warning: " + message);

        public static void Error(string message, Exception ex) => _log($"{message}\r\n{ex}");

        public static async Task Execution(string message, Func<Task> action)
        {
            var stopwatch = new Stopwatch();

            Info($"Start {message}");
            stopwatch.Start();

            await action.Invoke().ConfigureAwait(false);

            stopwatch.Stop();
            Info($"Finish {message} in {stopwatch.Elapsed}");
        }
    }
}
